/*
 * Copyright 2008 M-Way Solutions
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mwaysolutions.ocm;

import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


/**
 *
 * @author <a href="mailto:m.zdila@mwaysolutions.com">Martin Ždila</a>
 */
public class OcmTypeDescriptor implements Iterable<OcmPropertyDescriptor> {

	private final String nodeTypeName;
	private final Class<?> clazz;
	private final Map<String, OcmPropertyDescriptor> jcrPropertyNameToPropertyDescriptorMap = new HashMap<String, OcmPropertyDescriptor>();
	private final Map<String, OcmPropertyDescriptor> beanPropertyNameToPropertyDescriptorMap = new HashMap<String, OcmPropertyDescriptor>();
	private OcmPropertyDescriptor uuidPropertyDescriptor;
	private boolean hasUniqueProperty;


	public boolean isHasUniqueProperty() {
		return hasUniqueProperty;
	}


	public OcmTypeDescriptor(final String nodeTypeName, final Class<?> clazz) {
		this.nodeTypeName = nodeTypeName;
		this.clazz = clazz;
	}


	public void addProperty(final OcmPropertyDescriptor ocmPropertyDescriptor) {
		jcrPropertyNameToPropertyDescriptorMap.put(ocmPropertyDescriptor.getJcrPropertyName(), ocmPropertyDescriptor);
		beanPropertyNameToPropertyDescriptorMap.put(ocmPropertyDescriptor.getBeanPropertyName(), ocmPropertyDescriptor);
		if (ocmPropertyDescriptor.isUuid()) {
			uuidPropertyDescriptor = ocmPropertyDescriptor;
		}
		hasUniqueProperty |= ocmPropertyDescriptor.isUnique();
	}


	public void inheritProperties(final OcmTypeDescriptor superOcmTypeDescriptor) {
		jcrPropertyNameToPropertyDescriptorMap.putAll(superOcmTypeDescriptor.jcrPropertyNameToPropertyDescriptorMap);
		beanPropertyNameToPropertyDescriptorMap.putAll(superOcmTypeDescriptor.beanPropertyNameToPropertyDescriptorMap);
		if (uuidPropertyDescriptor == null) { // preventing nullification
			uuidPropertyDescriptor = superOcmTypeDescriptor.uuidPropertyDescriptor;
		}
	}


	public String getNodeTypeName() {
		return nodeTypeName;
	}


	public Class<?> getClazz() {
		return clazz;
	}


	public OcmPropertyDescriptor getPropertyDescriptorByJcrPropertyName(final String jcrPropertyName) {
		return jcrPropertyNameToPropertyDescriptorMap.get(jcrPropertyName);
	}


	public OcmPropertyDescriptor getPropertyDescriptorByBeanPropertyName(final String beanPropertyName) {
		final OcmPropertyDescriptor ocmPropertyDescriptor = beanPropertyNameToPropertyDescriptorMap.get(beanPropertyName);
		if (ocmPropertyDescriptor == null) {
			throw new IllegalArgumentException("undefined BEAN property: " + beanPropertyName);
		}
		return ocmPropertyDescriptor;
	}


	public OcmPropertyDescriptor getUuidPropertyDescriptor() {
		return uuidPropertyDescriptor;
	}


	@Override
	public Iterator<OcmPropertyDescriptor> iterator() {
		return Collections.unmodifiableCollection(beanPropertyNameToPropertyDescriptorMap.values()).iterator();
	}


}
